package org.lemon.ddd.domain.event.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lemon.ddd.common.utils.GsonUtil;
import org.lemon.ddd.domain.event.BaseDomainEvent;
import org.lemon.ddd.domain.event.DomainEventPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * 领域事件发布实现类
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class DomainEventPublisherImpl implements DomainEventPublisher {

    private final ApplicationEventPublisher applicationEventPublisher;

    @Override
    public <T> void publishEvent(BaseDomainEvent<T> event) {
        log.debug("发布事件,event:{}", GsonUtil.gsonToString(event));
        applicationEventPublisher.publishEvent(event);
    }
}